package arrays;

public class P2 {
    public static void main(String[] args) {
        int[] nums1 = new int[]{1 ,2};
        int[] nums2 = new int[]{3};
        System.out.println(findMedianSortedArrays(nums1, nums2));
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int len1 = nums1.length;
        int len2 = nums2.length;
        boolean b = (len1 + len2) % 2 == 0;
        double mad = (len1 + len2 + 0.0) / 2;
        int end = (int)(b ? mad + 1 : Math.ceil(mad));
        int i = 0, j = 0, k = 0;
        int[] ints = new int[end];
        while (k < end) {
            int i1 = i < len1 ? nums1[i] : Integer.MAX_VALUE;
            int i2 = j < len2 ? nums2[j] : Integer.MAX_VALUE;
            if (i1 < i2) {
                ints[k] = i1;
                i++;
            } else {
                ints[k] = i2;
                j++;
            }
            ++k;
        }
        if (b) {
            return (ints[end-2] + ints[end-1] + 0.0)/ 2;
        }
        return ints[end-1];
    }
}
